post.func.php API参考文档
# post.func.php API 参考文档
## 概述
`post.func.php` 是 Xiuno BBS 的帖子模型层,提供了帖子的 CRUD 操作、内容格式化、列表查询等核心功能。
## 原始 CURD 操作
### `post__create()`
#### 说明
```php
function post__create(array $arr, int $gid): int|false
```
创建帖子记录,最原生的插入操作,自动生成 message_fmt。
#### 参数
- `$arr`: 要插入的帖子数据数组,包含 message 等字段
- `$gid`: 用户组 ID
#### 返回值
- 成功返回插入的帖子 ID
- 失败返回 FALSE
#### 示例
```php
$arr = array(
'tid' => 1,
'isfirst' => 0,
'uid' => 1,
'create_date' => time(),
'userip' => ip2long('127.0.0.1'),
'message' => '测试回复',
'doctype' => 0
);
$pid = post__create($arr, 2);
```
### `post__update()`
#### 说明
```php
function post__update(int $pid, array $arr): int|false
```
更新帖子记录,最原生的更新操作。
#### 参数
- `$pid`: 帖子 ID
- `$arr`: 要更新的数据数组
#### 返回值
- 成功返回受影响的行数
- 失败返回 FALSE
#### 示例
```php
$update = array('message' => '更新后的回复');
$r = post__update(1, $update);
```
### `post__read()`
#### 说明
```php
function post__read(int $pid): array|false
```
读取帖子记录,最原生的查询操作。
#### 参数
- `$pid`: 帖子 ID
#### 返回值
- 成功返回帖子数组
- 失败返回 FALSE
#### 示例
```php
$post = post__read(1);
```
### `post__delete()`
#### 说明
```php
function post__delete(int $pid): int|false
```
删除帖子记录,最原生的删除操作。
#### 参数
- `$pid`: 帖子 ID
#### 返回值
- 成功返回受影响的行数
- 失败返回 FALSE
#### 示例
```php
$r = post__delete(1);
```
### `post__find()`
#### 说明
```php
function post__find(array $cond = array(), array $orderby = array(), int $page = 1, int $pagesize = 20): array
```
查找帖子列表,最原生的查询操作。
#### 参数
- `$cond`: 条件数组
- `$orderby`: 排序数组
- `$page`: 页码
- `$pagesize`: 每页数量
#### 返回值
- 返回帖子列表数组
#### 示例
```php
$postlist = post__find(array('tid' => 1), array('pid' => 1), 1, 20);
```
## 关联 CURD 操作
### `post_create()`
#### 说明
```php
function post_create(array $arr, int $fid, int $gid): int|false
```
创建帖子,包含关联操作(如更新主题统计、用户发帖数等)。
#### 参数
- `$arr`: 帖子数据数组,包含 tid、uid、message 等字段
- `$fid`: 板块 ID
- `$gid`: 用户组 ID
#### 返回值
- 成功返回帖子 ID
- 失败返回 FALSE
#### 示例
```php
$arr = array(
'tid' => 1,
'isfirst' => 0,
'uid' => 1,
'create_date' => time(),
'userip' => ip2long('127.0.0.1'),
'message' => '测试回复',
'doctype' => 0
);
$pid = post_create($arr, 1, 2);
```
### `post_update()`
#### 说明
```php
function post_update(int $pid, array $arr, int $tid = 0): bool
```
更新帖子,包含内容格式化等关联操作。
#### 参数
- `$pid`: 帖子 ID
- `$arr`: 要更新的数据数组
- `$tid`: 主题 ID(可选)
#### 返回值
- 成功返回 TRUE
- 失败返回 FALSE
#### 示例
```php
$update = array('message' => '更新后的回复');
$r = post_update(1, $update);
```
### `post_read()`
#### 说明
```php
function post_read(int $pid): array
```
读取帖子,包含数据格式化。
#### 参数
- `$pid`: 帖子 ID
#### 返回值
- 返回格式化后的帖子数组
#### 示例
```php
$post = post_read(1);
```
### `post_read_cache()`
#### 说明
```php
function post_read_cache(int $pid): array
```
从缓存中读取帖子,避免重复从数据库取数据。
#### 参数
- `$pid`: 帖子 ID
#### 返回值
- 返回帖子数组
#### 示例
```php
$post = post_read_cache(1);
```
### `post_delete()`
#### 说明
```php
function post_delete(int $pid): bool
```
删除帖子,包含清理关联数据(附件等)。
#### 参数
- `$pid`: 帖子 ID
#### 返回值
- 成功返回 TRUE
- 失败返回 FALSE
#### 示例
```php
$r = post_delete(1);
```
### `post_delete_by_tid()`
#### 说明
```php
function post_delete_by_tid(int $tid): int
```
删除主题下的所有帖子。
#### 参数
- `$tid`: 主题 ID
#### 返回值
- 返回删除的帖子数量
#### 示例
```php
$count = post_delete_by_tid(1);
```
### `post_delete_by_uid()`
#### 说明
```php
function post_delete_by_uid(int $uid): int|false
```
删除用户的所有帖子。
#### 参数
- `$uid`: 用户 ID
#### 返回值
- 成功返回受影响的行数
- 失败返回 FALSE
#### 示例
```php
$r = post_delete_by_uid(1);
```
### `post_find()`
#### 说明
```php
function post_find(array $cond = array(), array $orderby = array(), int $page = 1, int $pagesize = 20): array
```
查找帖子列表,包含数据格式化和楼层计算。
#### 参数
- `$cond`: 条件数组
- `$orderby`: 排序数组
- `$page`: 页码
- `$pagesize`: 每页数量
#### 返回值
- 返回格式化后的帖子列表数组
#### 示例
```php
$postlist = post_find(array('tid' => 1), array('pid' => 1), 1, 20);
```
### `post_find_by_tid()`
#### 说明
```php
function post_find_by_tid(int $tid, int $page = 1, int $pagesize = 50): array
```
按照主题 ID 查找帖子列表。
#### 参数
- `$tid`: 主题 ID
- `$page`: 页码
- `$pagesize`: 每页数量
#### 返回值
- 返回帖子列表数组
#### 示例
```php
$postlist = post_find_by_tid(1, 1, 50);
```
## 内容格式化
### `user_post_message_format()`
#### 说明
```php
function user_post_message_format(string &$s): void
```
格式化用户帖子内容。
#### 参数
- `$s`: 内容字符串(引用传递)
#### 返回值
- 无返回值,直接修改传入的字符串
#### 示例
```php
$s = '测试内容
';
user_post_message_format($s);
```
### `post_message_fmt()`
#### 说明
```php
function post_message_fmt(array &$arr, int $gid): void
```
写入时格式化帖子内容。
#### 参数
- `$arr`: 帖子数据数组(引用传递)
- `$gid`: 用户组 ID
#### 返回值
- 无返回值,直接修改传入的数组
#### 示例
```php
$arr = array('message' => '测试内容', 'doctype' => 0);
post_message_fmt($arr, 2);
```
### `post_brief()`
#### 说明
```php
function post_brief(string $s, int $len = 100): string
```
获取内容的简介。
#### 参数
- `$s`: 内容字符串
- `$len`: 简介长度,默认为 100
#### 返回值
- 返回内容简介
#### 示例
```php
$brief = post_brief('测试内容', 50);
```
### `post_quote()`
#### 说明
```php
function post_quote(int $quotepid): string
```
对内容进行引用。
#### 参数
- `$quotepid`: 被引用的帖子 ID
#### 返回值
- 返回引用的 HTML 内容
#### 示例
```php
$quote = post_quote(1);
```
### `post_highlight_keyword()`
#### 说明
```php
function post_highlight_keyword(string $str, string $k): string
```
高亮关键词。
#### 参数
- `$str`: 原始字符串
- `$k`: 关键词
#### 返回值
- 返回高亮后的字符串
#### 示例
```php
$highlighted = post_highlight_keyword('测试内容', '测试');
```
## 其他方法
### `post_count()`
#### 说明
```php
function post_count(array $cond = array()): int|false
```
统计帖子数量。
#### 参数
- `$cond`: 条件数组
#### 返回值
- 成功返回帖子数量
- 失败返回 FALSE
#### 示例
```php
$count = post_count(array('tid' => 1));
```
### `post_maxid()`
#### 说明
```php
function post_maxid(): int|false
```
获取帖子最大 ID。
#### 参数
- 无参数
#### 返回值
- 成功返回最大帖子 ID
- 失败返回 FALSE
#### 示例
```php
$maxid = post_maxid();
```
### `post_safe_info()`
#### 说明
```php
function post_safe_info(array $post): array
```
获取帖子安全信息(移除敏感字段)。
#### 参数
- `$post`: 帖子数组
#### 返回值
- 返回移除敏感字段后的帖子数组
#### 示例
```php
$safe_post = post_safe_info($post);
```
### `post_find_by_pids()`
#### 说明
```php
function post_find_by_pids(array $pids, array $order = array('pid'=>-1)): array
```
根据多个帖子 ID 查找帖子。
#### 参数
- `$pids`: 帖子 ID 数组
- `$order`: 排序数组
#### 返回值
- 返回帖子列表数组
#### 示例
```php
$postlist = post_find_by_pids(array(1, 2, 3));
```
### `post_file_list_html()`
#### 说明
```php
function post_file_list_html(array $filelist, bool $include_delete = FALSE): string
```
生成附件列表的 HTML。
#### 参数
- `$filelist`: 文件列表数组
- `$include_delete`: 是否包含删除链接
#### 返回值
- 返回附件列表的 HTML 字符串
#### 示例
```php
$html = post_file_list_html($filelist, TRUE);
```
### `post_format()`
#### 说明
```php
function post_format(array &$post): void
```
格式化帖子数据。
#### 参数
- `$post`: 帖子数组(引用传递)
#### 返回值
- 无返回值,直接修改传入的数组
#### 示例
```php
$post = post_read(1);
post_format($post);
```
### `post_list_access_filter()`
#### 说明
```php
function post_list_access_filter(array &$postlist, int $gid): void
```
对帖子列表进行权限过滤。
#### 参数
- `$postlist`: 帖子列表数组(引用传递)
- `$gid`: 用户组 ID
#### 返回值
- 无返回值,直接修改传入的数组
#### 示例
```php
$postlist = post_find_by_tid(1);
post_list_access_filter($postlist, 2);
```